﻿
using CommonTool;
using Models.WebChat;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Uin
{
    /// <summary>
    /// 单元管理
    /// </summary>
    public class UinInfoManager
    {
        private static List<UinInfo> _UinInfoList = null;
        private static int _Id = 100006;
        private static object _Lock = new object();
        static UinInfoManager()
        {
            _UinInfoList = new List<UinInfo> {
                new UinInfo{
                    Id=100000,
                    Uin_name="SysRobort",
                    Uin_password="password",
                    Uin_type= UinType.USER
                },
                new UinInfo{
                    Id=100001,
                    Uin_name="user1",
                    Uin_password="password",
                    Uin_type= UinType.USER
                },
                new UinInfo{
                    Id=100002,
                    Uin_name="user2",
                    Uin_password="password",
                    Uin_type=UinType.USER
                },
                new UinInfo{
                    Id=100003,
                    Uin_name="user3",
                    Uin_password="password",
                    Uin_type=UinType.USER
                },
                new UinInfo{
                    Id=100004,
                    Uin_name="全员群",
                    Uin_password="",
                    Uin_type=UinType.GROUP
                },
                new UinInfo{
                    Id=100005,
                    Uin_name="Group2",
                    Uin_password="",
                    Uin_type=UinType.GROUP
                }
            };
            //Logger.Log("开始载入用户数据");
            //载入用户信息
            var path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "bin/Data", Constant.UserData);
            LoadUserInfoFromConfig(path);
        }
        /// <summary>
        /// 添加单元信息
        /// </summary>
        /// <param name="uinInfo"></param>
        public static void AddUinInfo(UinInfo uinInfo)
        {
            //lock(_Lock)
            {
                uinInfo.Id = _Id++;
                _UinInfoList.Add(uinInfo);
            }
            
        }
        /// <summary>
        /// 从配置文件引入用户信息
        /// </summary>
        /// <param name="path"></param>
        public static void LoadUserInfoFromConfig(string path)
        {
           // Logger.Log("正在载入用户数据");
            var users = ConfigReader.ReadFromXml<UserInfoList>(path);
            users.Users.ForEach(u =>
            {
                //Logger.Log($"正在载入用户 :{u.Id}");
                AddUinInfo(u);
            });
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int Login(string name,string pwd)
        {
            var uinInfo = _UinInfoList.Where(u =>u.Uin_type == UinType.USER && u.Uin_name.Equals(name)).FirstOrDefault();
            if(uinInfo!=null)
            {
                if(uinInfo.Uin_password.Equals(pwd))
                {
                    return uinInfo.Id;
                }
            }
            return -1;
        }
        /// <summary>
        /// 获取所有用户列表
        /// </summary>
        /// <returns></returns>
        public List<UinInfo> GetAllUserInfo()
        {
            return _UinInfoList.Where(u => u.Uin_type== UinType.USER).Select(u => new UinInfo { Id = u.Id, Uin_name = u.Uin_name }).ToList();
        }
        /// <summary>
        /// 获取所有群
        /// </summary>
        /// <returns></returns>
        public List<UinInfo> GetAllGroupInfo()
        {
            return _UinInfoList.Where(u => u.Uin_type== UinType.GROUP).Select(u => new UinInfo { Id = u.Id, Uin_name = u.Uin_name }).ToList();
        }
        /// <summary>
        /// 根据单元id获取单元信息
        /// </summary>
        /// <param name="uinid"></param>
        /// <returns></returns>
        public UinInfo GetUinInfoById(int uinid)
        {
            return _UinInfoList.Where(u => u.Id == uinid).Select(u => new UinInfo { Id = u.Id, Uin_name = u.Uin_name,Uin_type = u.Uin_type }).FirstOrDefault();
        }
        /// <summary>
        /// 根据单元id获取单元信息列表
        /// </summary>
        /// <param name="uinid"></param>
        /// <returns></returns>
        public List<UinInfo> GetUinInfoListById(params int[] uinid)
        {
            return _UinInfoList.Where(u => uinid.Contains(u.Id)).Select(u => new UinInfo { Id = u.Id, Uin_name = u.Uin_name, Uin_type = u.Uin_type }).ToList();
        }
        /// <summary>
        /// 设置状态（本来打算设置用户的在线和下线状态，但是没用上）
        /// </summary>
        /// <param name="uinid"></param>
        /// <param name="state"></param>
        public void SetUinState(int uinid, UinState state)
        {
            var uinInfo = _UinInfoList.Where(u => u.Id == uinid).FirstOrDefault();
            if (uinInfo!=null)uinInfo.Uin_State = state;
        }

    }
}
